﻿@using CodeGenerator.Core
@using System.Text
@{
    int count = 0;
    string primaryKey = "";
    TableInfo table = Model.TableInfo;
    string mName = CodeGenerator.Common.Utils.Pascal(Model.Name);    
}
using Dian.NLogEx;
using Shengyi.Common;
using Shengyi.Kefu_ViewModel.Domain.SYWeb;
using Shengyi.Kefu_WEB;
using Shengyi.Model.Domain;
using Shengyi.Model.Domain.SYWeb;
using Shengyi.OA.WebCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;

namespace Kefu_WEB.Controllers.SYWeb
{
    [SessionState(SessionStateBehavior.ReadOnly)]
    public class @(mName)Controller : BaseControl
    {
        [CheckLogin(Order = 0)]
        [DbConnection(Order = 1)]
        public ActionResult List(HtmlViewCondition htmlView, @(mName)ListSearchVM search)
        {
            BaseCommon.CheckInitHtmlViewCondition(ref htmlView);
            htmlView.SortField = htmlView.SortField ?? "a.CreateTime";
            htmlView.SortOrder = FieldSortOrder.Desc;
            ViewBag.htmlView = htmlView;
            try
            {
                if (search.IsNull())
                {
                    search = new @(mName)ListSearchVM();
                }
                int totalSize = 0;
                var list = @(mName)Logic.Get@(mName)List(htmlView, search, out totalSize);
                ViewBag.List = list;
                ViewBag.PageView = BuildPageNavHtml.BuildV1(Request, totalSize, htmlView.CurrentPage, htmlView.PageSize);
                ViewBag.TotalSize = totalSize;
                ViewBag.HtmlView = htmlView;
                ViewBag.Search = search;                              

                //TODO:需要添加其他参数构造
            }
            catch (Exception ex)
            {
                Logger.Fatal(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, ex.Message, ex);
                ViewBag.Error = true;
            }
            return View();
        }

        [CheckLogin(Order = 0)]
        [DbConnection(Order = 1)]
        public ActionResult Detail(Guid id)
        {
            try
            {
                if (id.IsNotNullOrNotAll())
                {
                    OAResponseModel response = @(mName)Logic.Get@(mName)Model(id.Value);
                    if (response.code == 200)
                    {
                        var model = response.Extend as Edit@(mName)VM;
                        ViewBag.VModel = model;
                    }
                    else
                    {
                        id=Guid.Empty;
                        ViewBag.Error = true;
                        ViewBag.ErrorMessage = response.Message;
                    }
                }
                ViewBag.Id = id.IsNullOrAll()?Guid.Empty:id.Value;
            }
            catch (Exception ex)
            {
                Logger.Fatal(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, ex.Message, ex);
                ViewBag.Error = true;
            }
            return View();
        }

        [CheckLogin(Order = 0)]
        [DbConnection(Order = 1)]
        [HttpPost]
        public JsonResult SaveDetail(Edit@(mName)VM vm)
        {
            var result = new MyJsonResult();
            try
            {
                OAResponseModel rm = @(mName)Logic.Save@(mName)(vm);
                if (rm.code == 200)
                {
                    result.Extend = rm.Extend;
                    result.Succeed();
                }
                else
                {
                    result.Message = rm.Message;
                }
            }
            catch (DataErrorException ex)
            {
                Logger.Fatal(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, ex.Message, ex);
                result.Message = ex.Message;
            }
            catch (Exception ex)
            {
                Logger.Fatal(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, ex.Message, ex);
                result.Message = ShengYiTip.ServerError;

            }
            return Json(result);
        }

        [CheckLogin(Order = 0)]
        [DbConnection(Order = 1)]
        [HttpPost]
        public JsonResult Delete(Guid id)
        {
            var result = new MyJsonResult();
            try
            {
                OAResponseModel rm = @(mName)Logic.Delete@(mName)(id, true);
                if (rm.code == 200)
                {
                    result.Extend = rm.Extend;
                    result.Succeed();
                }
                else
                {
                    result.Message = rm.Message;
                }
            }
            catch (DataErrorException ex)
            {
                Logger.Fatal(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, ex.Message, ex);
                result.Message = ex.Message;
            }
            catch (Exception ex)
            {
                Logger.Fatal(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, ex.Message, ex);
                result.Message = ShengYiTip.ServerError;

            }
            return Json(result);
        }

        [CheckLogin(Order = 0)]
        [DbConnection(Order = 1)]
        [HttpPost]
        public JsonResult Delete(Guid id)
        {
            var result = new MyJsonResult();
            try
            {
                OAResponseModel rm = @(mName)Logic.Delete@(mName)(id, true);
                if (rm.code == 200)
                {
                    result.Extend = rm.Extend;
                    result.Succeed();
                }
                else
                {
                    result.Message = rm.Message;
                }
            }
            catch (DataErrorException ex)
            {
                Logger.Fatal(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, ex.Message, ex);
                result.Message = ex.Message;
            }
            catch (Exception ex)
            {
                Logger.Fatal(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, ex.Message, ex);
                result.Message = ShengYiTip.ServerError;

            }
            return Json(result);
        }
    }
}